package hp_approximation;

public class test {

    String[] Sa = new String[2];

    test(){
 
        System.out.println();
        Sa[0] = new String("hhhh");
        Sa[1] = new String("hhhh");
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "--hhhh";
        Sa[1] = "pphhhh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "pphhhh";
        Sa[1] = "--hhhh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "hh--hh";
        Sa[1] = "hhpphh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "hhpphh";
        Sa[1] = "hh--hh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "hhhhpp";
        Sa[1] = "hhhh--";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "hhhh----";
        Sa[1] = "hhhhpppp";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "hhpp--hh";
        Sa[1] = "hh--pphh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "pp--hh";
        Sa[1] = "--pphh";
        System.out.println(this.build_fold());

        System.out.println();
        Sa[0] = "pppp--hh--hhpppp";
        Sa[1] = "----pphhpphh----";
        System.out.println(this.build_fold());
 
    }

    public String build_fold() {

        String fold = new String();

        System.out.println(Sa[0]);
        System.out.println(Sa[1]);

        int s = Sa[0].length();
        int end = s;
        int i;

        // build the sequence for Sa[0]
        // I check if I have a gap block at the end of Sa[0]
        i = s-1;
        if(Sa[0].charAt(i)=='-') {
            int l = i;
            while(Sa[0].charAt(i)=='-')
                i--;
            l = l-i;
            end = i;
        }
        i = 1;
        // I check if I have a gap block at the begining of Sa[1]
        if(Sa[1].charAt(i)=='-') {
            int l = 0;
            while(Sa[1].charAt(i)=='-')
                i++;
            l = i - l;
            for(int j=0; j<(l/2)-1; j++)
                fold = fold.concat("n");
            fold = fold.concat("e");
            for(int j=0; j<(l/2); j++)
                fold = fold.concat("s");
            i++;
        }
        // I check if I have a gap block at the begining of Sa[0]
        if(Sa[0].charAt(i)=='-') {
            while(Sa[0].charAt(i)=='-')
                i++;
            i++;
        }
        // now treat the rest of the sequence
        while(i<end) {
            if(Sa[1].charAt(i)!='-') {
                if(i<end&&Sa[1].charAt(i)!='-'&&Sa[0].charAt(i)!='-')
                    fold = fold.concat("e");
                i++;
                if(i<end&&Sa[0].charAt(i)=='-')
                    while(i<s&&Sa[0].charAt(i)=='-')
                        i++;
                else
                    if(i==s)
                    // I'm done with Sa[0], I go to Sa[1]
                    fold = fold.concat("s");
            }
            else {
                // I have a gap block in Sa[1]
                int l = i;
                while(i<end&&(Sa[1].charAt(i)=='-'))
                    i++;
                l = i-l;
                // I first check if it's at the end of Sa[1]
                if(i==s) {
                    for(int j=0; j<(l/2); j++)
                        fold = fold.concat("e");
                    fold = fold.concat("s");
                    for(int j=0; j<(l/2); j++)
                        fold = fold.concat("w");
                    // I'm done with Sa[0] and I went to Sa[1]
                }
                else {
                    for(int j=0; j<(l/2); j++)
                        fold = fold.concat("n");
                    fold = fold.concat("e");
                    for(int j=0; j<(l/2); j++)
                        fold = fold.concat("s");
                    // if I have gaps in Sa[0], I skip them
                    if(Sa[0].charAt(i)=='-') {
                        while(Sa[0].charAt(i)=='-')
                            i++;
                    }
                    i++;
                }
            }
        }

        
        // build the sequence for Sa[1]
        i = s-1;
        // I first deal with the gap block at the end of Sa[0] - if I have one
        if(end!=s) {
            fold = fold.concat("e");
            int l = i;
            while(Sa[0].charAt(i)=='-')
                i--;
            l = l-i;
            for(int j=0; j<(l/2); j++)
                fold = fold.concat("e");
            fold = fold.concat("s");
            for(int j=0; j<(l/2); j++)
                fold = fold.concat("w");
        }
        // I deal with the gap block at the end of Sa[1] - if I have one
        else
            while(Sa[1].charAt(i)=='-')
                i--;
        
        // now I can build the rest of Sa[1]
        while(i>1) {
            if(Sa[0].charAt(i-1)!='-') {
                //System.out.println(fold+" I will add a w "+i);
                fold = fold.concat("w");
                i--;
                //System.out.println("I decreased i to "+i);
                //System.out.println("I check for gaps in Sa[1] "+i+" "+Sa[1].charAt(i));
                if(i>0&&Sa[1].charAt(i-1)=='-')
                    while(i>0&&Sa[1].charAt(i-1)=='-')
                        i--;
                //System.out.println("Did I have any gaps? "+i);
            }
            else {
                // I have a gap block in Sa[0]
                int l = i;
                i--;
                while(i>0&&(Sa[0].charAt(i)=='-'))
                    i--;
                l = l-i;
                for(int j=0; j<(l/2); j++)
                    fold = fold.concat("s");
                fold = fold.concat("w");
                // I check if the gap block is at the begining or I have a gap block in Sa[1]
                if(i==0||Sa[1].charAt(i)=='-') {
                    int j = i;
                    while(j>0&&Sa[1].charAt(j)=='-')
                        j--;
                    if(j==0||j==i)
                        for(j=0; j<(l/2)-1; j++)
                            fold = fold.concat("n");
                    else
                        for(j=0; j<(l/2); j++)
                            fold = fold.concat("n");
                    if(j!=i)
                        i = j;
                }
                else
                    for(int j=0; j<(l/2); j++)
                        fold = fold.concat("n");
            }
        }
        if(i==1&&Sa[1].charAt(i)!='-') 
            fold = fold.concat("w");

        return fold;
    }

}
